//
//  HtmlViewController.m
//  UIWebView
//
//  Created by keisun on 2017/6/19.
//  Copyright © 2017年 keisun. All rights reserved.
//

#import "HtmlViewController.h"

@interface HtmlViewController ()<UIWebViewDelegate>

@end

@implementation HtmlViewController

/**
 iOS : Xcode
 Java : eclipse\MyEclipse
 Android : eclipse\MyEclipse\Android Studio
 网页（前端）：Dreamweaver（美工）、Sublime（前端）、WebStorm（前端）
 
 iOS App == Native + HTML5
 
 完整的网页组成：
 HTML:内容（文字、图片）
 CSS:美化、样式
 JS:动态效果、事件处理、跟用户进行交互
 */

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    webView.delegate = self;
    [self.view addSubview:webView];
    [webView loadRequest:[NSURLRequest requestWithURL:[[NSBundle mainBundle] URLForResource:@"index" withExtension:@"html"]]];
}

-(void)call{
    NSLog(@"%s", __func__);
//    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tel://10086"]];
}

-(void)sendMessage{
    NSLog(@"%s", __func__);
}

-(void)openCamera{
    NSLog(@"%s", __func__);
}


#pragma mark - <UIWebViewDelegate>

-(void)webViewDidFinishLoad:(UIWebView *)webView{
//    [webView stringByEvaluatingJavaScriptFromString:@"alert(100);"];
    
    // 利用JS获得当前网页的标题
//    NSString *title = [webView stringByEvaluatingJavaScriptFromString:@"document.title;"];
//    NSLog(@"TITLE:%@", title);
    
//    NSString *result = [webView stringByEvaluatingJavaScriptFromString:@"login();"];
//    NSLog(@"result-----%@", result);
}


/**
 通过这个方法完成JS调用OC
 JS和OC交互的第三方框架：WebViewJavaScriptBridge
 */
-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
    
    NSString *url = request.URL.absoluteString;
    NSLog(@"url:%@",url);
    NSString *scheme = @"xmg://";
    if ([url hasPrefix:scheme]) {
        
        NSString *methodName = [url substringFromIndex:scheme.length];
        [self performSelector:NSSelectorFromString(methodName) withObject:nil];
        return NO;
    }
    return YES;
}
@end
